<?php

require_once(APPLICATION_PATH.'/models/Record.php');
class RecordRes
{
    //分页查找所要显示的数据
    public function recordListRes($search, $page=1, $numPerPage=8)
    {
        $where =" WHERE";
        !empty($search) ? $where .= " count_tag LIKE '%".$search."%'" : $where .=" 1";
        
        //创建记录对象
        $recordModel = new Record();
        $db = $recordModel->getAdapter();
        
        //获得总共有多少页
        $counts = $db->fetchOne(
            "SELECT COUNT(*) FROM record_countpasswd".$where
        );
    
        //抓取所要显示的数据
        $limit = $numPerPage;
        $offset = ($page-1)*$numPerPage;

        $select = $db->select();
        $select->from('record_countpasswd', '*');
        $select->where('count_tag LIKE ?', '%'.$search.'%');
        $select->orWhere('count_name LIKE ?', '%'.$search.'%');
        $select->order('id');
        $select->limit($limit, $offset);
        
        $sql = $select->__toString();
        $result = $db->fetchAll($sql);
        //!(empty($result)) ? $info = '' : $info = '没有记录！';
        
        //factory出一个分页并且设置当前页码与每页显数
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($counts));
        $paginator->setCurrentPageNumber($page)
                  ->setItemCountPerPage($numPerPage);
        $res = array(
                'dbres' => $result,
                'paginator' => $paginator
        );
        return $res;
    }
    
}
